package cn.iocoder.yudao.module.contract.dal.mysql.orderContract;

import java.util.*;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.OrderContractDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.vo.CompanyOrderContractVO;
import cn.iocoder.yudao.module.contract.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.contract.controller.admin.orderContract.vo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.parameters.P;

/**
 * 订单合同 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface OrderContractMapper extends BaseMapperX<OrderContractDO> {

//    default PageResult<OrderContractDO> selectPage(OrderContractPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<OrderContractDO>()
//                .likeIfPresent(OrderContractDO::getContractTypeCode, reqVO.getContractTypeCode())
//                .likeIfPresent(OrderContractDO::getContractTypeName, reqVO.getContractTypeName())
//                .eqIfPresent(OrderContractDO::getContractTemplateId, reqVO.getContractTemplateId())
//                .likeIfPresent(OrderContractDO::getContractTemplateName, reqVO.getContractTemplateName())
//                .eqIfPresent(OrderContractDO::getOrderId, reqVO.getOrderId())
//                .eqIfPresent(OrderContractDO::getContractHtml, reqVO.getContractHtml())
//                .eqIfPresent(OrderContractDO::getContractFileUrl, reqVO.getContractFileUrl())
//                .eqIfPresent(OrderContractDO::getDownloadCount, reqVO.getDownloadCount())
//                .eqIfPresent(OrderContractDO::getPrintCount, reqVO.getPrintCount())
//                .eqIfPresent(OrderContractDO::getIsPushRelease, reqVO.getIsPushRelease())
//                .eqIfPresent(OrderContractDO::getStatus, reqVO.getStatus())
//                .betweenIfPresent(OrderContractDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(OrderContractDO::getId));
//    }

    default PageResult<OrderContractPageRespVO> selectPage(OrderContractPageReqVO reqVO) {
        List<String> htdymbList = new ArrayList<>();
        if("gx".equals(reqVO.getListType())){
            //购销
            htdymbList.add("Y1");
            htdymbList.add("Y2");
        }else if("hyj".equals(reqVO.getListType())){
            //合约价
            htdymbList.add("Y5");
        }else if("thxy".equals(reqVO.getListType())){
            //退货换货
            htdymbList.add("Y6");
        }else if("bjd".equals(reqVO.getListType())){
            //报价单
            htdymbList.add("Y3");
        }else if("bcxy".equals(reqVO.getListType())){
            //补充协议
            htdymbList.add("1");
        }else if("all".equals(reqVO.getListType())){
        }else{
            htdymbList.add(reqVO.getListType());
        }
        if(StringUtils.isNotBlank(reqVO.getZterm()) && (","+reqVO.getZterm()+",").contains("xyz")){
            reqVO.setZterm(reqVO.getZterm()+",X011,X012,X013,X014,X015,X017,X020");
        }
        return selectJoinPage(reqVO,OrderContractPageRespVO.class, new MPJLambdaWrapperX<OrderContractDO>() // 查询 List
                        .betweenIfPresent(OrderContractDO::getCreateTime, reqVO.getCreateTime())
                        .selectAll(OrderContractDO.class) // 查询 system_users 表的 all 所有字段// 联表 WHERE system_users.dept_id = system_dept.id
                        .selectAssociation(OrderDO.class, OrderContractPageRespVO::getOrderDO) // 查询 system_dept 表的 name 字段，使用 deptName 字段“部分”返回
                        .like(!ObjectUtils.isEmpty(reqVO.getContractTypeCode()),OrderContractDO::getContractTypeCode, reqVO.getContractTypeCode())
                        .eq(!ObjectUtils.isEmpty(reqVO.getIsPushRelease()),OrderContractDO::getIsPushRelease, reqVO.getIsPushRelease())
                        .eq(!ObjectUtils.isEmpty(reqVO.getStatus()),OrderContractDO::getStatus, reqVO.getStatus())
                        .eq(!ObjectUtils.isEmpty(reqVO.getOrderStatus()),OrderContractDO::getOrderStatus, reqVO.getOrderStatus())
                        .isNotNull(htdymbList.size()==0,OrderContractDO::getContractTemplateId)
                        .ge("1".equals(reqVO.getIsPrint()),OrderContractDO::getPrintCount,reqVO.getIsPrint())
                        .eq("0".equals(reqVO.getIsPrint()),OrderContractDO::getPrintCount,reqVO.getIsPrint())
                        .and("1".equals(reqVO.getRecycleStatus()),i->i.isNull(OrderContractDO::getFileIds).or().eq(OrderContractDO::getFileIds,""))
                        .and("1".equals(reqVO.getRecycleStatus()),i->i.isNull(OrderContractDO::getScanFileIds).or().eq(OrderContractDO::getScanFileIds,""))
                        .and("2".equals(reqVO.getRecycleStatus()),i->i.isNull(OrderContractDO::getFileIds).or().eq(OrderContractDO::getFileIds,""))
                        .and("2".equals(reqVO.getRecycleStatus()),i->i.isNotNull(OrderContractDO::getScanFileIds).ne(OrderContractDO::getScanFileIds,""))
                        .and("3".equals(reqVO.getRecycleStatus()),i->i.isNotNull(OrderContractDO::getFileIds).ne(OrderContractDO::getFileIds,""))
                        .eq(!ObjectUtils.isEmpty(reqVO.getOrderFreezeStatus()),OrderContractDO::getOrderFreezeStatus, reqVO.getOrderFreezeStatus())
                        .leftJoin(OrderDO.class, OrderDO::getId, OrderContractDO::getOrderId)
                        .in(htdymbList.size()>0,OrderDO::getHtdymb,htdymbList)
                        .eq(!ObjectUtils.isEmpty(reqVO.getHtdymb()),OrderDO::getHtdymb, reqVO.getHtdymb())
                        .between(!ObjectUtils.isEmpty(reqVO.getAudat()),OrderDO::getAudat, ArrayUtils.get(reqVO.getAudat(), 0),ArrayUtils.get(reqVO.getAudat(), 1))
//                .like(!ObjectUtils.isEmpty(reqVO.getAudat()),OrderDO::getAudat, reqVO.getAudat())
                        .like(!ObjectUtils.isEmpty(reqVO.getVbeln()) && "1".equals(reqVO.getVbelnType()),OrderDO::getVbeln, reqVO.getVbeln())
                        .in(!ObjectUtils.isEmpty(reqVO.getVbeln()) && "2".equals(reqVO.getVbelnType()),OrderDO::getVbeln, !ObjectUtils.isEmpty(reqVO.getVbeln()) ?reqVO.getVbeln().split(","): new Object[]{""})

                        .like(!ObjectUtils.isEmpty(reqVO.getZtermt()) && "1".equals(reqVO.getZtermtType()),OrderDO::getZtermt, reqVO.getZtermt())
                        .in(!ObjectUtils.isEmpty(reqVO.getZtermt()) && "2".equals(reqVO.getZtermtType()),OrderDO::getZtermt, !ObjectUtils.isEmpty(reqVO.getZtermt()) ?reqVO.getZtermt().split(","): new Object[]{""})
                        .notIn(!ObjectUtils.isEmpty(reqVO.getZtermt()) && "3".equals(reqVO.getZtermtType()),OrderDO::getZtermt, !ObjectUtils.isEmpty(reqVO.getZtermt()) ?reqVO.getZtermt().split(","): new Object[]{""})

                        .ge(!ObjectUtils.isEmpty(reqVO.getVbelnStart()),OrderDO::getVbeln, reqVO.getVbelnStart())
                        .le(!ObjectUtils.isEmpty(reqVO.getVbelnEnd()),OrderDO::getVbeln, reqVO.getVbelnEnd())
                        .like(!ObjectUtils.isEmpty(reqVO.getVkorg()),OrderDO::getVkorg, reqVO.getVkorg())
                        .in(!ObjectUtils.isEmpty(reqVO.getZzhtgklxms()),OrderDO::getZzhtgklxms,  !ObjectUtils.isEmpty(reqVO.getZzhtgklxms()) ?reqVO.getZzhtgklxms().split(","): new Object[]{""})
                        .in(!ObjectUtils.isEmpty(reqVO.getPzinfo()),OrderDO::getAuartt,  !ObjectUtils.isEmpty(reqVO.getPzinfo()) ?reqVO.getPzinfo().split(","): new Object[]{""})
                        .in(!ObjectUtils.isEmpty(reqVO.getBuinfo()),OrderDO::getButxt,  !ObjectUtils.isEmpty(reqVO.getBuinfo()) ?reqVO.getBuinfo().split(","): new Object[]{""})
                        .in(!ObjectUtils.isEmpty(reqVO.getKuinfo()),OrderDO::getKname,  !ObjectUtils.isEmpty(reqVO.getKuinfo()) ?reqVO.getKuinfo().split(","): new Object[]{""})

                        .and(!ObjectUtils.isEmpty(reqVO.getKunnr()) && "1".equals(reqVO.getKunnrType()),i-> i.like(OrderDO::getKname, reqVO.getKunnr()).or().like(OrderDO::getKunnr, reqVO.getKunnr()))
                        .and(!ObjectUtils.isEmpty(reqVO.getKunnr()) && "2".equals(reqVO.getKunnrType()),i-> i.in(OrderDO::getKname, !ObjectUtils.isEmpty(reqVO.getKunnr()) ?reqVO.getKunnr().split(","): new Object[]{""}).or().in(OrderDO::getKunnr, !ObjectUtils.isEmpty(reqVO.getKunnr()) ?reqVO.getKunnr().split(","): new Object[]{""}))

                        .in(!ObjectUtils.isEmpty(reqVO.getVkinfo()),OrderDO::getVkburt,  !ObjectUtils.isEmpty(reqVO.getVkinfo()) ?reqVO.getVkinfo().split(","): new Object[]{""})
                        .and(!ObjectUtils.isEmpty(reqVO.getVkginfo()),i -> i.like(OrderDO::getVkgrp, reqVO.getVkginfo()).or().like(OrderDO::getVkgrpt, reqVO.getVkginfo()))
                        .in(!ObjectUtils.isEmpty(reqVO.getYwyinfo()),OrderDO::getYwyt,  !ObjectUtils.isEmpty(reqVO.getYwyinfo()) ?reqVO.getYwyinfo().split(","): new Object[]{""})

                        .in(!ObjectUtils.isEmpty(reqVO.getZzhtgklx()),OrderDO::getZzhtgklx,  !ObjectUtils.isEmpty(reqVO.getZzhtgklx()) ?reqVO.getZzhtgklx().split(","): new Object[]{""})
                        .in(!ObjectUtils.isEmpty(reqVO.getZterm()),OrderDO::getZterm,  !ObjectUtils.isEmpty(reqVO.getZterm()) ?reqVO.getZterm().split(","): new Object[]{""})

                        .orderByDesc(ObjectUtils.isEmpty(reqVO.getOrder()),OrderDO::getAudat).orderByDesc(ObjectUtils.isEmpty(reqVO.getOrder()),OrderDO::getVbeln)
                        .orderBy(ObjectUtils.isNotEmpty(reqVO.getOrder()),"ascending".equals(reqVO.getOrder()),getFieldName(reqVO.getProp()))
        );
    }

    default String getFieldName(String prop){
        prop = prop.replace("orderDO.","t1.");
        return  StringUtils.toUnderScoreCase(prop);
    }

    /**
     * 获取未上传合同附件的订单合同
     *
     * @return
     */
    List<OrderContractDO> listUnUploadContractFile();

    /**
     * 获取在指定日期三个月内的订单合同信息
     *
     * @param intervalDate
     * @param excludeContractIds 排查的合同Id
     * @return
     */
    List<CompanyOrderContractVO> listCompanyOrderContract(@Param("clientCode") String clientCode, @Param("intervalDate") String intervalDate, @Param("excludeContractIds") List<Long> excludeContractIds);
}